<?php
class MY_Controller extends CI_Controller
{
    public $ceeconfigArray;//cee配置
    public $userinfoArray; //用户信息
    public $isloginArray; // 用户是否登录
	function __construct(){
		parent::__construct();
		//判断用户的登录情况   $islogin
		if(empty($_GET['openid'])||empty($_GET['openkey'])||empty($_GET['pf'])) {
		    //
		}else{
		    $userdata = array("openid" =>$this->stringSet($_GET['openid']),
		                    "openkey"=>$this->stringSet($_GET['openkey']),
		                    "pf"     =>$this->stringSet($_GET['pf'])
		    );
		    $this->session->set_userdata($userdata);
		}
		//调用配置类
		$this->config->load('ceeconfig',TRUE);
		$this->ceeconfigArray = $this->config->item('ceeconfig');
		//调用tx接口
		$this->load->library('Ceeclass');
		//cee参数
		$this->ceeclass->openid  = $this->session->userdata('openid');
		$this->ceeclass->openkey = $this->session->userdata('openkey');
		$this->ceeclass->pf      = $this->session->userdata('pf');
		$this->ceeclass->gp = array('openid'=>$this->ceeclass->openid,'openkey'=>$this->ceeclass->openkey,'pf'=>$this->ceeclass->pf);
	    //是否登录
		$isloginArray2 = $this->isloginArray = $this->ceeclass->islogin();
		if ($isloginArray2['ret']==0) {//已经登录
			//链接跳转
			if(isset($_GET['source'])){
				$_source = explode("_", $_GET['source']);
				//竞拍
				if ($_source[0]=="auction") {
					redirect(base_url("?c=auction"));
					exit();
				}
				//节日专题
				if ($_source[0]=="zhuanti") {
					redirect(base_url("?c=zhuanti"));
					exit();
				}
				//交叉推广
				if ($_source[0]=="fromnht"){
					$this->session->set_userdata('linkfrom', 'neihantu');
					redirect(base_url("?c=nhtzs"));
					exit();
				} 
				//说说商品排第一
				if($_source[0]=="jingxuan"){
					if ($_source[1]=='type') {
						redirect(base_url("?c=main&type=".$_source[2]));
					}elseif($_source[1]=='id'){
						redirect(base_url("?c=main&id=".$_source[2]));
					}
					exit();
				}
				if($_source[0]=="jiujiu"){
					redirect(base_url("?c=npn&id=".$_source[1]));
					exit();
				}
			}
			//a.调用数据库  1.用户change  2.用户info  3.用户收藏
    		$this->load->model('User_model','_usermodel');
    		$this->_usermodel->userchange();//1. 用户change
    		$this->userinfoArray['web'] = $this->_usermodel->userInfo();//用户info
    		$this->userinfoArray['collected'] = $this->_usermodel->collected();//3.用户收藏
    		//b.用户tx信息  
    		if(!empty($_GET['openid'])){
    			//调用接口 用户tx信息
    			$this->userinfoArray['tx'] = $this->ceeclass->getuserinfo();
    			$this->session->set_userdata('userinfoTX', $this->userinfoArray['tx']);
    		}else{
    			$this->userinfoArray['tx'] = $this->session->userdata('userinfoTX');
    		}
    		//用户关注的QQ   $qqArray = array('2337959476','861121','783350315','2202411011','80249498');
    		$qqArray = array('783350315');
    		$txData['fanQQ'] = $this->needfunQQ($this->ceeclass, $qqArray);
    		//是否添加到面板
    		$txData['isAppOnPanel'] = $this->ceeclass->isapponpanel();
    		$this->userinfoArray['txData'] = json_encode($txData);
		}else{//未登录
			redirect(base_url("?c=relogin"));
		}
	}
	
	/* --------------------------------------------------------- */
	//处理字符串
	private function stringSet($str){
		return addSlashes(htmlspecialchars($str));
	}	
	//获取ip地址
	function GetIP(){
	    if(getenv('HTTP_CLIENT_IP')){
	        $ip = getenv('HTTP_CLIENT_IP');
	    }elseif(getenv('HTTP_X_FORWARDED_FOR')){
	        $ip = getenv('HTTP_X_FORWARDED_FOR');
	    }elseif(getenv('HTTP_X_FORWARDED')){
	        $ip = getenv('HTTP_X_FORWARDED');
	    }elseif(getenv('HTTP_FORWARDED_FOR')){
	        $ip = getenv('HTTP_FORWARDED_FOR');
	    }elseif(getenv('HTTP_FORWARDED')){
	        $ip = getenv('HTTP_FORWARDED');
	    }else{
	        $ip = $_SERVER['REMOTE_ADDR'];
	    }
	    return $ip;
	}
	//需要关注的QQ
	function needfunQQ($TXApi,$qqArray){
	    if (empty($qqArray)) {
	        return array('is_fan'=>'','fan_qq'=>'','fan_qq_url'=>'');
	    }
	    $is_fan = 1;$fan_qq = '';$fan_qq_url='';
	    foreach ($qqArray as $item){
	        $is_fans = $TXApi->isQQFans($item);
	        if ($is_fans == 0) {
	            $is_fan= 0;
	            $fan_qq = $item;
	            $fan_qq_url = "http://open.qzone.qq.com/like?url=http%3A%2F%2Fuser.qzone.qq.com%2F".$item."&type=button&width=400&height=30&style=3";
	            break;
	        }
	    }
	    //return array('is_fan'=>$is_fan,'fan_qq'=>$fan_qq,'fan_qq_url'=>$fan_qq_url);
	    return array('is_fan'=>$is_fan,'fan_qq'=>$fan_qq);
	}
}